﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Atomic.Common;
using Atomic.Common.Validators;
using System.Linq.Expressions;
using System.Web.Mvc;
using System.Configuration;
using System.Reflection;
using System.Data.Common;
using Atomic.Permission.Entities;
using NHibernate;
using NHibernate.Criterion;
using NHibernate.Linq;
using Atomic.Db;
using Atomic.Permission.DAL;

namespace Atomic.Permission.BLL
{
    public class RoleBLL : ErrorImpl, IRole
    {
        /// <summary>
        /// 见基类
        /// </summary>
        /// <param name="pager"></param>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public virtual Pager<RoleEntity> GetPager(Pager pager, Expression<Func<RoleEntity, bool>> predicate = null)
        {
            RoleDAL roleDAL = new RoleDAL();
            return roleDAL.GetPager(pager, predicate);
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="id">角色Id</param>
        /// <returns></returns>
        public virtual bool Remove(int id)
        {
            RoleDAL roleDAL = new RoleDAL();
            roleDAL.Remove(id);
            return true;
        }

        /// <summary>
        /// 添加新角色
        /// </summary>
        /// <param name="entity">角色实体</param>
        /// <returns>是否添加成功</returns>
        public virtual bool Add(RoleEntity entity)
        {
            //实体验证
            if (!(VResult = entity.Validate()).Success)
            {
                Error = new ErrorResult()
                {
                    Message = VResult.FailureMessage
                };
                return false;
            }

            RoleDAL roleDAL = new RoleDAL();
            roleDAL.Add(entity);
            return true;
        }

        /// <summary>
        /// 更新角色信息
        /// </summary>
        /// <param name="entity">角色实体</param>
        /// <param name="id">角色ID</param>
        /// <returns>是否更新成功</returns>
        public bool Update(RoleEntity entity, int id)
        {
            //实体验证
            if (!(VResult = entity.Validate()).Success)
            {
                Error = new ErrorResult()
                {
                    Message = VResult.FailureMessage
                };
                return false;
            }

            RoleDAL roleDAL = new RoleDAL();
            roleDAL.Update(entity, id);
            return true;
        }

        ///// <summary>
        ///// 角色功能/操作点关系
        ///// </summary>
        ///// <param name="id">角色ID</param>
        ///// <returns></returns>
        //public List<RoleFunOperateRelationDbEntity> GetFunOperates(int id)
        //{
        //    List<RoleFunOperateRelationDbEntity> list = null;
        //    try
        //    {
        //        using (dbc = new EntitesContainer())
        //        {
        //            list = dbc.RoleFunOperateRelation.Where(m => m.RoleID == id)
        //                .ToList() ?? new List<RoleFunOperateRelationDbEntity>();
        //        }
        //        return list;
        //    }
        //    catch (Exception ex)
        //    {
        //        return null;
        //    }
        //}

        ///// <summary>
        ///// 保存角色的功能点关系
        ///// </summary>
        ///// <param name="roleID">角色ID</param>
        ///// <param name="entitys">功能/操作点关系</param>
        //public void SaveFunOperates(int roleID, List<RoleFunOperateRelationDbEntity> entitys)
        //{
        //    using (dbc = new EntitesContainer())
        //    {
        //        dbc.Connection.Open();
        //        DbTransaction trans = dbc.Connection.BeginTransaction();
        //        try
        //        {
        //            List<RoleFunOperateRelationDbEntity> olds = dbc.RoleFunOperateRelation.Where(m => m.RoleID == roleID)
        //                .ToList();
        //            //删除
        //            foreach (RoleFunOperateRelationDbEntity item in olds)
        //            {
        //                dbc.RoleFunOperateRelation.DeleteObject(item);
        //            }
        //            //添加
        //            for (int i = 0; i < entitys.Count; i++)
        //            {
        //                entitys[i].RoleID = roleID;
        //                dbc.RoleFunOperateRelation.AddObject(entitys[i]);
        //            }
        //            dbc.SaveChanges();
        //            trans.Commit();
        //            return;
        //        }
        //        catch (Exception ex)
        //        {
        //            trans.Rollback();
        //            return;
        //        }
        //        finally
        //        {
        //            dbc.Connection.Close();
        //        }
        //    }
        //}

        /// <summary>
        /// 查询角色基本信息
        /// </summary>
        /// <param name="id">角色ID</param>
        /// <returns></returns>
        public RoleEntity Get(int id)
        {
            RoleDAL roleDAL = new RoleDAL();
            return roleDAL.Get(id);
        }
    }
}
